<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所属能源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="energyTypesId">
              <a-select placeholder="请选择所属能源" v-model="model.energyTypesId" @change="getequipmentTypes">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option v-for="(item, key) in tdEnergyTypes" :key="key" :value="item.id">
                  <span style="display: inline-block; width: 100%" :title="item.energyName">
                    {{ item.energyName }}
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="预警类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warningtypeId">
              <j-dict-select-tag
                type="list"
                v-model="model.warningtypeId"
                dictCode="ew_warningtype,ew_type,id"
                placeholder="请选择预警类型"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备选择" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipmentId">
              <a-select placeholder="请选择设备" v-model="model.equipmentId" @change="equipmentChange">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option v-for="(item, key) in equipmentTypes" :key="key" :value="item.id">
                  <span style="display: inline-block; width: 100%" :title="item.dmCode">
                    {{ item.dmCode }}
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="预警名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ewName">
              <a-input v-model="model.ewName" placeholder="请输入预警名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="预警规则（预警阀值）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="ewValue"
            >
              <a-input v-model="model.ewValue" placeholder="请输入预警规则（预警阀值）"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="预警规则（预警阀值单位）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="unit"
            >
              <a-input v-model="model.unit" placeholder="请输入预警规则（预警阀值单位）"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启动状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startupStatus">
              <j-switch v-model="model.startupStatus"></j-switch>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'EwRulesForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        equipmentId: '',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/rules/ewRules/add',
        edit: '/rules/ewRules/edit',
        queryById: '/rules/ewRules/queryById',
      },
      tdEnergyTypes: [],
      equipmentTypes: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    this.gettdEnergyTypes()
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    gettdEnergyTypes() {
      getAction('/tdEnergytypes/tdEnergyTypes/listAll').then((res) => {
        if (res.success) {
          this.tdEnergyTypes = res.result
          this.getequipmentTypes(this.model.energyTypesId)
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    getequipmentTypes(value) {
      let Obj = this.tdEnergyTypes.find((item) => item.id == value)
      if (Obj) {
        this.model.equipmentType = Obj.theStandby1
        getAction('/rules/ewRules/queryList', {
          code: Obj.theStandby1,
        }).then((res) => {
          if (res.success) {
            if (!this.model.id) {
              this.model.equipmentId = ''
            }
            this.equipmentTypes = res.result
          } else {
            this.$message.warning(res.message)
          }
        })
      }
    },
    equipmentChange(value) {
      this.model.dmCode = this.equipmentTypes.find(item => item.id == value).dmCode
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>